package com.gua.jbs.utility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gua.jbs.controller.LoginController;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfGenerator {
	private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
			Font.BOLD);
	private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			Font.NORMAL, BaseColor.RED);
	private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
			Font.BOLD);
	private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			Font.BOLD);
	private static Font tableHeaderFont = new Font(Font.FontFamily.TIMES_ROMAN,
			9, Font.BOLD);
	private static Font tableCellFont = new Font(Font.FontFamily.TIMES_ROMAN,
			8, Font.NORMAL);
	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	private static void createTable(Section subCatPart, List dataList, Map<String, String> custValueMap)
			throws BadElementException, DocumentException {
		
		logger.info("Table being  created");
		if (dataList != null && dataList.isEmpty() != true) {
			
			/*Set the Column size*/
			PdfPTable table = new PdfPTable(6);

			/**
			 * Table Header Section Start
			 */
			PdfPCell headerCell = new PdfPCell(new Phrase("S. No.",tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Product Code",tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Name", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Quantity", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Coupon", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);

			headerCell = new PdfPCell(new Phrase("Amount", tableHeaderFont));
			headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(headerCell);
			table.setHeaderRows(1);
			/**
			 * Table Header Section Close
			 */

			/**
			 * Table Data Section Start
			 */
			PdfPCell dataCell = null;
			for (int counter = 0; counter < dataList.size(); counter++) {
				Object[] data = (Object[]) dataList.get(counter);
				dataCell = new PdfPCell(new Phrase("" + (counter + 1),tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[0] != null && !data[0].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[0],tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[1] != null && !data[1].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[1],tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[2] != null && !data[2].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[2],tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[3] != null && !data[3].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[3],tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);

				if (data[4] != null && !data[4].equals(""))
					dataCell = new PdfPCell(new Phrase("" + data[4],tableCellFont));
				else
					dataCell = new PdfPCell(new Phrase("-", tableCellFont));
				dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(dataCell);
			}
			/**
			 * Table Data Section End
			 */

			/**
			 * Other Information Section Start
			 */
			dataCell = new PdfPCell(new Phrase("Total Amount", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(5);
			table.addCell(dataCell);
			// TO DO
			dataCell = new PdfPCell(new Phrase(custValueMap.get(ApplicationConstant.TOTAL_AMOUNT), tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);

			dataCell = new PdfPCell(new Phrase("Vat 1%", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(5);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase(custValueMap.get(ApplicationConstant.SERIVCE_TAX), tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);
			
			dataCell = new PdfPCell(new Phrase("Discount", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(5);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase(custValueMap.get(ApplicationConstant.discount), tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);

			dataCell = new PdfPCell(new Phrase("Misc & Courier Charges", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(5);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase(custValueMap.get(ApplicationConstant.courierCharges), tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);
			
			dataCell = new PdfPCell(new Phrase("Grand Total", tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			dataCell.setColspan(5);
			table.addCell(dataCell);
			dataCell = new PdfPCell(new Phrase(custValueMap.get(ApplicationConstant.FINAL_AMOUNT), tableCellFont));
			dataCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(dataCell);
			/**
			 * Other Information Section End
			 */

			subCatPart.add(table);
		}
		logger.info("Table created");
	}

	public static void addContent(Document document, Map<String, String> custValueMap, Map<String, List<String>> orderValueMap) throws DocumentException {
		logger.info("Content being written");
		// Anchor anchor = new Anchor("First Chapter", catFont);
		Anchor anchor = new Anchor("Bill Information");
		anchor.setName("Bill Information");

		// Second parameter is the number of the chapter
		Chapter catPart = new Chapter(new Paragraph(anchor), 1);

		Paragraph subPara = new Paragraph("Subcategory 1");
		Section subCatPart = catPart.addSection(subPara);
		subCatPart.add(new Paragraph("Hello"));

		subPara = new Paragraph("Subcategory 2", subFont);
		subCatPart = catPart.addSection(subPara);
		subCatPart.add(new Paragraph("Name: "+custValueMap.get(ApplicationConstant.custName)));
		subCatPart.add(new Paragraph("Address: "+custValueMap.get(ApplicationConstant.custAddress)));
		subCatPart.add(new Paragraph("Mobile: "+custValueMap.get(ApplicationConstant.custMobile)));
		subCatPart.add(new Paragraph("Email Address: "+custValueMap.get(ApplicationConstant.emailAddress)));
		subCatPart.add(new Paragraph("Reference No: "+custValueMap.get(ApplicationConstant.REF_NO)));
		subCatPart.add(new Paragraph("Date: "+new Date()));
		subCatPart.add(new Paragraph("Invoice No: "+custValueMap.get(ApplicationConstant.INVOICE_NO)));
		subCatPart.add(new Paragraph("Payment Mode: "+custValueMap.get(ApplicationConstant.custPayMode)));

		// add a list
		// createList(subCatPart);
		Paragraph paragraph = new Paragraph();
		addEmptyLine(paragraph, 5);
		subCatPart.add(paragraph);

		// add a table
		logger.info("orderValueMap--->"+orderValueMap.size());
		List dataList = new ArrayList();
		Object[] dataObject = null;
		for(int i=0;i<orderValueMap.size();i++){
			List ordervalueList=orderValueMap.get(ApplicationConstant.ORDER + i);
			dataObject =new Object[ordervalueList.size()];
			for(int j=0;j<ordervalueList.size();j++){
				dataObject[j]=ordervalueList.get(j);
			}
			dataList.add(dataObject);
		}
		createTable(subCatPart, dataList, custValueMap);
		// now add all this to the document
		document.add(catPart);

		logger.info("Content written");
	}

	private static void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
}
